﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YJKFamilyLibDataManager;

namespace YJKFamilyLibManager.FamilyLibModule
{
	public partial class DlgAddFamily : System.Windows.Forms.Form
	{
		public DlgAddFamily(DlgMain main)
		{
			this.mForm = main;
			this.InitializeComponent();
			this.ofd.InitialDirectory = this.mForm.initialDirectory;
			if (this.mForm.CurrentSubject != null)
			{
				this.cbxSubject.Items.Add(this.mForm.CurrentSubject);
				this.cbxSubject.SelectedIndex = 0;
				if (this.mForm.CurrentCategory != null)
				{
					this.cbxCategory.Items.Add(this.mForm.CurrentCategory);
					this.cbxCategory.SelectedIndex = 0;
				}
				else
				{
					this.cbxCategory.Items.Add(this.mForm.CurrentSubject);
					this.cbxCategory.SelectedIndex = 0;
				}
			}
			this.lbxRecordParam.DoubleClick += this.lbxRecordParam_DoubleClick;
		}

		private void lbxRecordParam_DoubleClick(object sender, EventArgs e)
		{
			if (this.lbxRecordParam.SelectedItem != null)
			{
				int selectedIndex = this.lbxRecordParam.SelectedIndex;
				this.lbxRecordParam.Items.RemoveAt(selectedIndex);
			}
		}

		private void cbxSubject_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			if (this.isMulti)
			{
				try
				{
					bool flag = false;
					foreach (string fileName in this.ofd.FileNames)
					{
						FileInfo fileInfo = new FileInfo(fileName);
						this.tbxPath.Text = fileInfo.FullName;
						int length = fileInfo.Name.LastIndexOf(".");
						this.tbxName.Text = fileInfo.Name.Substring(0, length);
						int startIndex = this.ofd.FileName.LastIndexOf("\\");
						this.mForm.initialDirectory = this.ofd.FileName.Substring(startIndex);
						this.readFamilyFile();
						if (this.isValidate && !this.cmdAddFamily((DbCategory)this.cbxCategory.SelectedItem))
						{
							flag = false;
						}
					}
					if (flag)
					{
						MessageBox.Show("添加完成。", "族库管理");
					}
					goto IL_147;
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.Message);
					goto IL_147;
				}
			}
			try
			{
				if (this.isValidate)
				{
					bool flag2 = this.cmdAddFamily((DbCategory)this.cbxCategory.SelectedItem);
					if (flag2)
					{
						MessageBox.Show("添加完成。", "族库管理");
					}
				}
			}
			catch (Exception ex2)
			{
				MessageBox.Show(ex2.Message);
			}
			IL_147:
			base.Close();
		}

		private bool cmdAddFamily(DbCategory category)
		{
			List<DbParamDefinition> paramDefinitionList = category.GetParamDefinitionList();
			foreach (DbParamDefinition dbParamDefinition in paramDefinitionList)
			{
				bool flag = false;
				foreach (object obj in this.lbxRecordParam.Items)
				{
					DbParamDefinition dbParamDefinition2 = (DbParamDefinition)obj;
					if (dbParamDefinition2.Name == dbParamDefinition.Name)
					{
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					throw new Exception("族中不存在分类关键参数：" + dbParamDefinition.Name + "。\n请添加后再入库");
				}
			}
			FileInfo fileInfo = new FileInfo(this.tbxPath.Text.Trim());
			Document document = this.mForm.UiApp.ActiveUIDocument.Document;
			Family family = FamilyUtil.ReloadFamily(document, fileInfo.FullName);
			DbFamily dbFamily = null;
			DbFamily dbFamily2 = category.Database.FamilyTable[family.Name];
			if (dbFamily2 != null)
			{
				MessageBox.Show("族已经包含在族库中，不能重复添加。");
				return false;
			}
			dbFamily = new DbFamily(this.tbxName.Text, this.pbxView.Image);
			dbFamily.Version = this.mForm.UiApp.Application.VersionNumber;
			dbFamily.SetDate(DateTime.Now);
			foreach (object obj2 in this.lbxInstanceParam.Items)
			{
				DbParamDefinition item = (DbParamDefinition)obj2;
				dbFamily.ParamDefinitionList.Add(item);
			}
			foreach (object obj3 in this.lbxRecordParam.Items)
			{
				DbParamDefinition item2 = (DbParamDefinition)obj3;
				dbFamily.ParamDefinitionList.Add(item2);
			}
			dbFamily.Description = this.rtxDescription.Text;
			dbFamily.Name = this.tbxName.Text;
			if (this.pbxLarge.Image == null)
			{
				dbFamily.LargeImage = Resource1._null;
			}
			else
			{
				dbFamily.LargeImage = this.pbxLarge.Image;
			}
			dbFamily.TwoDImage = Resource1._null;
			this.doAddFamily(category, dbFamily, fileInfo, document, family);
			return true;
		}

		private void doAddFamily(DbCategory category, DbFamily family, FileInfo fi, Document doc, Family fam)
		{
			try
			{
				if (category.AddFamily(family, fi))
				{
					family.RecordTypes(this.mForm.UiApp.ActiveUIDocument.Document, category.Database, fi.FullName);
					this.btnAdd.Enabled = false;
					this.mForm.lvFamily.LargeImageList.Images.Add(family.Name, family.Image);
					this.mForm.lvFamily.SmallImageList.Images.Add(family.Name, family.Image);
					ListViewItem listViewItem = new ListViewItem();
					listViewItem.Text = family.Name;
					listViewItem.Tag = family;
					listViewItem.ImageKey = family.Name;
					ListViewItem.ListViewSubItem listViewSubItem = new ListViewItem.ListViewSubItem();
					listViewSubItem.Text = family.Version;
					listViewItem.SubItems.Add(listViewSubItem);
					ListViewItem.ListViewSubItem listViewSubItem2 = new ListViewItem.ListViewSubItem();
					listViewSubItem2.Text = family.Date.ToString();
					listViewItem.SubItems.Add(listViewSubItem2);
					ListViewItem.ListViewSubItem listViewSubItem3 = new ListViewItem.ListViewSubItem();
					listViewSubItem3.Text = category.Name;
					listViewItem.SubItems.Add(listViewSubItem3);
					ListViewItem.ListViewSubItem listViewSubItem4 = new ListViewItem.ListViewSubItem();
					listViewSubItem4.Text = family.Count.ToString();
					listViewItem.SubItems.Add(listViewSubItem4);
					ListViewItem.ListViewSubItem listViewSubItem5 = new ListViewItem.ListViewSubItem();
					listViewSubItem5.Text = "";
					listViewItem.SubItems.Add(listViewSubItem5);
					ListViewItem.ListViewSubItem listViewSubItem6 = new ListViewItem.ListViewSubItem();
					listViewSubItem6.Text = family.Description;
					listViewItem.SubItems.Add(listViewSubItem6);
					this.mForm.lvFamily.Items.Add(listViewItem);
					this.tbxPath.Text = "";
					this.tbxName.Text = "";
					this.pbxView.Image = null;
					this.btnAdd.Enabled = false;
					this.lbxRecordParam.Items.Clear();
					this.lbxInstanceParam.Items.Clear();
					this.propertyGrid1.SelectedObject = null;
					this.rtxDescription.Text = "";
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void btnOpen_Click(object sender, EventArgs e)
		{
			this.ofd.Filter = "族文件|*.rfa";
			this.ofd.RestoreDirectory = true;
			this.ofd.Multiselect = true;
			this.ofd.FilterIndex = 1;
			if (this.ofd.ShowDialog() == DialogResult.OK)
			{
				List<string> list = new List<string>();
				foreach (string text in this.ofd.FileNames)
				{
					FileInfo fileInfo = new FileInfo(text);
					if (fileInfo.IsReadOnly)
					{
						list.Add(fileInfo.Name);
					}
					try
					{
						this.mForm.UiApp.Application.OpenDocumentFile(text);
					}
					catch
					{
						MessageBox.Show("所选文件中存在更高版本的Revit文件。");
						return;
					}
				}
				if (list.Count > 0)
				{
					string text2 = "";
					for (int j = 0; j < list.Count; j++)
					{
						if (j == 0)
						{
							text2 = "\"" + list[0] + "\"";
						}
						else
						{
							text2 = text2 + "、\"" + list[j] + "\"";
						}
					}
					MessageBox.Show("不能添加只读的族文件" + text2 + "。");
					return;
				}
				FamilyUtil.ClearFamilyDataForAddFamily(this.mForm.UiApp, this.ofd.FileNames);
				this.btnAdd.Enabled = true;
				this.ofd.FileName.LastIndexOf("\\");
				this.tbxPath.Text = (this.mForm.initialDirectory = this.ofd.FileName);
				FileInfo fileInfo2 = new FileInfo(this.ofd.FileNames[0]);
				int length = fileInfo2.Name.LastIndexOf(".");
				this.tbxName.Text = fileInfo2.Name.Substring(0, length);
				if (this.ofd.FileNames.Count<string>() > 1)
				{
					this.isMulti = true;
					for (int k = 1; k < this.ofd.FileNames.Count<string>(); k++)
					{
						FileInfo fileInfo3 = new FileInfo(this.ofd.FileNames[k]);
						int length2 = fileInfo3.Name.LastIndexOf(".");
						this.tbxName.Text = this.tbxName.Text + ";" + fileInfo3.Name.Substring(0, length2);
					}
				}
				this.readFamilyFile();
			}
		}

		private void readFamilyFile()
		{
			FamilyLibDatabase database = this.mForm.CurrentSubject.Database;
			DbCategory currentCategory = this.mForm.CurrentCategory;
			try
			{
				if (!FamilyUtil.GetFamilyPreview(this.mForm.UiApp, this.tbxPath.Text, this.pbxView))
				{
					this.isValidate = false;
				}
				else
				{
					this.isValidate = true;
					FamilyUtil.GetFamilyTypeParams(this.mForm.UiApp, this.tbxPath.Text, this.lbxRecordParam, currentCategory);
					FamilyUtil.GetFamilyInstanceParams(this.mForm.UiApp, this.tbxPath.Text, this.lbxInstanceParam, this.lbxRecordParam);
					this.pbxLarge.Image = FamilyUtil.GetLargeImage(this.mForm.UiApp.Application, this.tbxPath.Text);
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		public static DlgAddFamily GetInstance(DlgMain main)
		{
			if (DlgAddFamily.daf == null || DlgAddFamily.daf.IsDisposed)
			{
				DlgAddFamily.daf = new DlgAddFamily(main);
				DlgAddFamily.daf.MdiParent = main.ActiveMdiChild;
			}
			return DlgAddFamily.daf;
		}

		private void cbxCategory_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void button1_Click(object sender, EventArgs e)
		{
		}

		private void btnDel_Click(object sender, EventArgs e)
		{
			if (this.lbxRecordParam.SelectedItem != null)
			{
				int selectedIndex = this.lbxRecordParam.SelectedIndex;
				this.lbxRecordParam.Items.RemoveAt(selectedIndex);
			}
		}

		private void lbxRecordParam_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.lbxRecordParam.SelectedItem != null)
			{
				this.btnDel.Enabled = true;
				this.propertyGrid1.SelectedObject = (DbParamDefinition)this.lbxRecordParam.SelectedItem;
				return;
			}
			this.btnDel.Enabled = false;
		}

		private void tbxParamName_TextChanged(object sender, EventArgs e)
		{
			Regex regex = new Regex("^\\w");
			if (regex.Match(this.tbxParamName.Text).Success)
			{
				this.btnAddDef.Enabled = true;
				return;
			}
			this.btnAddDef.Enabled = false;
		}

		private void lbxInstanceParam_DoubleClick(object sender, EventArgs e)
		{
			if (this.lbxInstanceParam.SelectedItem != null)
			{
				int selectedIndex = this.lbxInstanceParam.SelectedIndex;
				this.lbxInstanceParam.Items.RemoveAt(selectedIndex);
			}
		}

		private void tbxName_TextChanged(object sender, EventArgs e)
		{
		}

		private void tbxName_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar != '~' || e.KeyChar != '!' || e.KeyChar != '@' || e.KeyChar != '#' || e.KeyChar != '$' || e.KeyChar != '%' || e.KeyChar != '^' || e.KeyChar != '&' || e.KeyChar != '*' || e.KeyChar != '(' || e.KeyChar != ')' || e.KeyChar != '{' || e.KeyChar != '}' || e.KeyChar != '"' || e.KeyChar != '\'' || e.KeyChar != '|' || e.KeyChar != '\\' || e.KeyChar != ';' || e.KeyChar != ',' || e.KeyChar != '<' || e.KeyChar != '>' || e.KeyChar != '?' || e.KeyChar != '/')
			{
				e.Handled = true;
			}
		}

		private void DlgAddFamily_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private static DlgAddFamily daf;

		private DlgMain mForm;

		private OpenFileDialog ofd = new OpenFileDialog();

		private List<DirectoryInfo> directoryList = new List<DirectoryInfo>();

		private bool isMulti;

		private bool isValidate;
	}
}
